<?php
namespace Admin\Controller;

use Common\Model\CommonModel;
use Think\Controller;

class PhotoController extends BaseController{
    public function index(){

        //主页
        $photoModel=M('photo');
        //var_dump($photoModel);exit;
        if(!empty($_GET['photo_cate'])){
            unset($_SESSION['like']['photo']);
            $_SESSION['like']['photo']=$_GET['photo_cate'];
            $condition1['name']=array('like',"%".$_GET['photo_cate']."%");
            $photocate=M('photocate')->where($condition1)->select();
            $news_cate=array();
            foreach($photocate as $key=>$value){
                $news_cate[]=$value['id'];
            }
            if(!empty($photo_cate)){
                $condition['photo_cate']=array('in',$photo_cate);
            }else{
                $this->display();
                exit;
            }
        }
        if(!empty($_GET)){
            unset($_SESSION['like']['photo']);
            $_SESSION['like']['photo']= $condition['photo_cate'];
        }
        $count = $photoModel->where($condition)->count();
        $page= new \Think\Page($count,5);
        $page->setConfig('next','下一页');
        $page->setConfig('prev','上一页');
        //显示还有多少条
        $page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page->show();
        $list=$photoModel->where($condition)->order(array('create'=>'desc'))->limit($page->firstRow.','.$page->listRows)->select();
        $list=CommonModel::photo($list);
        //var_dump($list);exit;
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->display();
    }


    public function create(){
        if(empty($_POST)){
            $photocateModel=D('Photocate');
            $cates=$photocateModel->getAll();
            //var_dump($cates);exit;
            $this->assign('cates',$cates);
        }
        $this->display();
    }


    public function doCreate(){
        if(empty($_POST['name'])){
            $this->error('图片名不能为空');
        }
        $data['name']=$_POST['name'];
        if(empty($_POST['photo_cate'])){
            $this->error('分类名不能为空');
        }
        $data['photo_cate']=$_POST['photo_cate'];
        $data['remark']=$_POST['remark'];
        $data['url']=$_POST['url'];
        $data['pic']=$this->upload();
        $data['create']=time();

        $photoModel=D('photo');
        $res=$photoModel->create($data);
        if($res){
            $this->success('添加成功',U('index'));
        }else{
            $this->error('添加失败');
        }

    }

    public function update(){
        $id=$_GET['id'];
        $photoModel=D('photo');
        $list=$photoModel->finded('id='.$id);
        $photocateModel=D('Photocate');
        $cates=$photocateModel->getAll();
        $list['pic']=CommonModel::substr($list['pic']);
        //var_dump($list);
        //var_dump($cates);
        $this->assign('cates',$cates);
        $this->assign('list',$list);
        $this->display();
    }

    public function doUpdate(){
        if(empty($_POST['name'])){
            $this->error('图名不能为空');
            exit;
        }
        $data['name']=$_POST['name'];
        if(empty($_POST['photo_cate'])){
            $this->error('分类不能为空');
        }
        $data['photo_cate']=$_POST['photo_cate'];
        $data['remark']=$_POST['remark'];
        if(!$_FILES['pic']['error']==4){
            $data['layout']=$this->upload();
        }
        $data['url']=$_POST['url'];
        $data['status']=$_POST['status'];
        $data['update']=time();
        $id=$_POST['id'];
        $where='id='.$id;
        $photoModel=D('photo');
        $list=$photoModel->finded($where);
        $data=CommonModel::filter($data,$list);
        // var_dump($data);exit;
        $res=$photoModel->update($where,$data);
        if($res){
            $this->success('修改成功',U('index'));
        }else{
            $this->error('修改失败');
        }
    }

    //删除
    public function del(){
        $photoModel=D('photo');
        $id=$_GET['id'];
        //var_dump($id);exit;
        $res= $photoModel->deleted('id='.$id);
        if($res){
            $this->success('删除成功',U('index'));
        }else{
            $this->error('删除失败');
        }

    }



}

